package com.ssmk.module.home.view;


import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.heima.easysp.SharedPreferencesUtils;
import com.liaoinstan.springview.widget.SpringView;
import com.ssmk.R;
import com.ssmk.adapter.HomeRecyclerAdapter;
import com.ssmk.base.MVPBaseFragment;
import com.ssmk.module.home.bean.Home_bean;
import com.ssmk.module.home.presenter.Home_present;
import com.ssmk.module.home.utils.GsonUtils;
import com.ssmk.utlis.CustomProgressDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by chengqianlang on 2017/6/28.
 */

public class Home_fragment extends MVPBaseFragment implements Home_View_Interface {

    @BindView(R.id.xrecycle_homef)
    RecyclerView mXrecycleHomef;
    Unbinder unbinder;
    @BindView(R.id.home_spview)
    SpringView mHomeSpview;
    @BindView(R.id.home_no_network)
    LinearLayout mNetWork;
    @BindView(R.id.home_fragment_nonetwork_tv)
    TextView mTvSetNetWork;

    Unbinder unbinder1;
    @BindView(R.id.empty_iv)
    ImageView emptyIv;
    @BindView(R.id.home_content)
    RelativeLayout homeContent;
    private boolean mIsconnect;
    private Home_present mHomePresent;
    private String mHomejson;
    private CustomProgressDialog mDialog;

    @Override
    public void UpData(Object o) {

    }

    @Override
    protected void initdata() {
//实例化P层
        mHomePresent = new Home_present(this, getActivity());
        if (mIsconnect) {
            mHomePresent.senddata();
        } else {
            // Toast.makeText(getActivity(), "当前网络不可用", Toast.LENGTH_SHORT).show();
            loadSpfData();
        }
    }



    @Override
    protected void initView() {
        mIsconnect = NetworkUtils.isConnected();
        mTvSetNetWork.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //NetworkUtils.openWirelessSettings();
                //  startActivityForResult(new Intent(Settings.ACTION_WIRELESS_SETTINGS),1);
                Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
                startActivityForResult(intent, 1); // 此为设置完成后返回到获取界面
            }
        });


        mDialog = new CustomProgressDialog(getActivity(), "", R.drawable.emptyloadanim);

        //取得spf中的数据
        mHomejson = SharedPreferencesUtils.init(getActivity(), "HomeData").getString("homejson");
        LogUtils.e("aa1", mHomejson);
        Log.e("TAG1234567", mHomejson);

        //recyclerview的上拉刷新
        mHomeSpview.setType(SpringView.Type.FOLLOW);
        mHomeSpview.setGive(SpringView.Give.TOP);
        mHomeSpview.setHeader(new Home_RefreshHead());
        mHomeSpview.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {

                if (mIsconnect) {
                    mHomeSpview.setVisibility(View.VISIBLE);
                    mNetWork.setVisibility(View.GONE);
                }
                mHomePresent.senddata();

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mHomeSpview.onFinishFreshAndLoad();
                    }
                }, 2000);
            }

            @Override
            public void onLoadmore() {

            }
        });

    }

    @Override
    protected int layoutRes() {
        return R.layout.home_fragment;

    }

    @Override
    public void setRecyclerAdapter(List<Home_bean.AppNewsBean> appNews, List<Home_bean.ShopsCarouselBean> shopsCarousel, List<Home_bean.ShopsRecommendBean> shopsRecommend) {
        initRecycleView(appNews, shopsCarousel, shopsRecommend);
    }

    //加载recyclerview
    private void initRecycleView(List<Home_bean.AppNewsBean> appNews, List<Home_bean.ShopsCarouselBean> shopsCarousel, List<Home_bean.ShopsRecommendBean> shopsRecommend) {
        mDialog.dismiss();
        HomeRecyclerAdapter adapter = new HomeRecyclerAdapter(getActivity(), appNews, shopsCarousel, shopsRecommend);
        mXrecycleHomef.setAdapter(adapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        mXrecycleHomef.setLayoutManager(linearLayoutManager);
    }

    //加载SharedPreferences中的数据
    private void loadSpfData() {
        if (!TextUtils.isEmpty(mHomejson)) {
            Home_bean homebean = (Home_bean) new GsonUtils(mHomejson, Home_bean.class).jsonToGson();
            List<Home_bean.AppNewsBean> appNews = homebean.getAppNews();
            List<Home_bean.ShopsCarouselBean> shopsCarousel = homebean.getShopsCarousel();
            List<Home_bean.ShopsRecommendBean> shopsRecommend = homebean.getShopsRecommend();
            initRecycleView(appNews, shopsCarousel, shopsRecommend);
        } else {
            //判断网络是否可用

            if (!mIsconnect) {
                mNetWork.setVisibility(View.VISIBLE);
                mHomeSpview.setVisibility(View.GONE);
                return;
            } else {
                mNetWork.setVisibility(View.GONE);
                mHomeSpview.setVisibility(View.VISIBLE);
            }
            // mHomePresent.senddata();

            mDialog.show();
            WindowManager windowManager = getActivity().getWindowManager();
            Display display = windowManager.getDefaultDisplay();
            WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes();
//        lp.alpha = 0.9f; //0.0-1.0//设置透明度?
            lp.width = display.getWidth(); //设置宽度
            lp.height = display.getHeight() - 380;
            mDialog.getWindow().setAttributes(lp);

        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder1.unbind();
    }

    @Override
    public void startIntentSenderForResult(IntentSender intent, int requestCode, @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) throws IntentSender.SendIntentException {
        super.startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask, flagsValues, extraFlags, options);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 0) {
            if (mIsconnect) {

            } else {
                // Toast.makeText(getActivity(), "当前网络不可用", Toast.LENGTH_SHORT).show();
                loadSpfData();
            }

        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder1 = ButterKnife.bind(this, rootView);
        return rootView;
    }
    @Override
    public void onStart() {
        super.onStart();
        mIsconnect = NetworkUtils.isConnected();
        if (mIsconnect) {
            mHomeSpview.setVisibility(View.VISIBLE);
            mNetWork.setVisibility(View.GONE);
            mHomePresent.senddata();
        }


    }

}
